/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.service.provider;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import io.jboot.aop.annotation.Bean;
import io.jboot.db.model.Column;
import io.jboot.db.model.Columns;
import io.jboot.utils.DateUtil;
import io.jpress.commons.service.JPressServiceBase;
import io.jpress.model.Utm;
import io.jpress.service.UtmService;
import io.jpress.service.task.UtmBatchSaveTask;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

@Bean
public class UtmServiceProvider extends JPressServiceBase<Utm> implements UtmService {

    private static final String[] chinaProvinces = new String[]{
            "北京",
            "天津",
            "上海",
            "重庆",
            "河北",
            "河南",
            "云南",
            "辽宁",
            "黑龙江",
            "湖南",
            "安徽",
            "山东",
            "新疆",
            "江苏",
            "浙江",
            "江西",
            "湖北",
            "广西",
            "甘肃",
            "山西",
            "内蒙古",
            "陕西",
            "吉林",
            "福建",
            "贵州",
            "广东",
            "青海",
            "西藏",
            "四川",
            "宁夏",
            "海南",
            "台湾",
            "香港",
            "澳门",
    };


    @Override
    public void doRecord(Utm utm) {
        UtmBatchSaveTask.record(utm);
    }

    @Override
    public Page<Utm> _paginateByUserId(int page, int pagesize, long userId) {
        return DAO.paginateByColumn(page, pagesize, Column.create("user_id", userId), "created desc");
    }

    @Override
    public Map<String, Long> queryIpIn7Days() {
        Map<String, Long> ret = new LinkedHashMap<>();
        for (int i = 6; i >= 0; i--) {
            Date startOfDay = DateUtil.getStartOfDay(DateUtil.addDays(new Date(), -i));
            Date endOfDay = DateUtil.getEndOfDay(DateUtil.addDays(new Date(), -i));

            String sql = "select count(distinct ip) from utm " +
                    "where created > '" + DateUtil.toDateTimeString(startOfDay) + "' " +
                    "and created <'" + DateUtil.toDateTimeString(endOfDay) + "'";

            Long count = Db.queryLong(sql);
            ret.put(DateUtil.toDateString(startOfDay), count == null ? 0 : count);
        }
        return ret;
    }

    @Override
    public Map<String, Long> queryIpMapIn7Days() {
        Map<String, Long> ret = new LinkedHashMap<>();
        for (String chinaProvince : chinaProvinces) {
            String sql = "select count(distinct ip) from utm " +
                    "where created > '" + DateUtil.toDateTimeString(DateUtil.addDays(new Date(), -6)) + "' " +
                    "and ip_region_province = '" + chinaProvince + "'";
            Long count = Db.queryLong(sql);
            ret.put(chinaProvince, count == null ? 0 : count);
        }
        return ret;
    }


    @Override
    public Map<String, Long> queryUvIn7Days() {
        Map<String, Long> ret = new LinkedHashMap<>();
        for (int i = 6; i >= 0; i--) {
            Date startOfDay = DateUtil.getStartOfDay(DateUtil.addDays(new Date(), -i));
            Date endOfDay = DateUtil.getEndOfDay(DateUtil.addDays(new Date(), -i));

            String sql = "select count(distinct anonym) from utm " +
                    "where created > '" + DateUtil.toDateTimeString(startOfDay) + "' " +
                    "and created <'" + DateUtil.toDateTimeString(endOfDay) + "'";

            Long count = Db.queryLong(sql);
            ret.put(DateUtil.toDateString(startOfDay), count == null ? 0 : count);
        }
        return ret;
    }


    @Override
    public Map<String, Long> queryPvIn7Days() {
        Map<String, Long> ret = new LinkedHashMap<>();
        for (int i = 6; i >= 0; i--) {
            Date startOfDay = DateUtil.getStartOfDay(DateUtil.addDays(new Date(), -i));
            Date endOfDay = DateUtil.getEndOfDay(DateUtil.addDays(new Date(), -i));

            String sql = "select count(*) from utm " +
                    "where created > '" + DateUtil.toDateTimeString(startOfDay) + "' " +
                    "and created <'" + DateUtil.toDateTimeString(endOfDay) + "'";

            Long count = Db.queryLong(sql);
            ret.put(DateUtil.toDateString(startOfDay), count == null ? 0 : count);
        }
        return ret;
    }


    @Override
    public Long queryPvBySiteId(Long siteId, Date startTime, Date endTime) {
        return DAO.findCountByColumns(Columns.create("site_id", siteId)
                .gt("created", startTime)
                .lt("created", endTime));
    }
}